<template>
	<view class="container">
		<view class="contenter">
			<text>{{$t('zhujici.zhujici1')}}</text>
		</view>

		<view class="content">
			<view class="content-title">
				<text>{{$t('zhujici.zhujici2')}}</text>
			</view>
			<view class="row">
				<view class="item" v-for="(item, index) in array" :key="index" @click="rowitem(item)">
					{{ item }}
				</view>
			</view>
		</view>
		<view class="content">
			<view class="content-title">
				<text>{{$t('zhujici.zhujici10')}}</text>
			</view>
			<view class="row">
				<view class="item" v-for="(item, rowIndex) in wordsList" :key="rowIndex" @click="add(item)">
					{{ item }}
				</view>
			</view>
		</view>


		<view class="anniu" @click="goyanzeng">
			<text>{{$t('zhujici.zhujici11')}}</text>
		</view>
		<!-- 以下是该组件使用的全部属性 -->
		<sanshui-payment-password v-model="showModal" :mode="mode" :digits="6"  />
	</view>


</template>

<script>
	//引入组件文件
	import SanshuiPaymentPassword from '../../components/sanshui-payment-password/sanshui-payment-password.vue'
	import {
		Debounce
	} from "@/api/debounce.js"; //防抖
	export default {
		components: {
			SanshuiPaymentPassword
		},
		data() {
			return {
				// 假设这是我们的初始数组，长度为 12
				array: [],
				wordsList: [],
				showModal: false,
				mode: 2,
				type: 1
			};
		},
		onLoad(e) {
			uni.setNavigationBarTitle({
				title: this.$t('yzphere'), // 必填，页面标题）
			})
			this.type = e.type
			// console.log('打印type', this.type);
			this.getarry()
		},
		methods: {
			// Fisher-Yates 算法实现数组顺序打乱，打乱助记词
			getarry() {
				let data = {}
				let opts = {
					url: '/user/Getzjc',
					method: 'GET'
				};
				this.api.httpTokenRequest(opts, data).then(res => {
					// console.log('获取助记词', res);
					if (res[1].data.state == 'success') {
						this.wordsList = res[1].data.list
					}

				})
			},
			rowitem(item) {
				// console.log('打印内容', item);
			},
			add(item) {
				if (this.array.length < 12 && !this.array.includes(item)) {
					this.array.push(item);
				} else if (this.array.includes(item)) {
					this.$modal.msg(this.$t('zhujici.zhujici12'))
				} else {
					// console.log('数组已满，无法添加更多元素');
				}
			},
			goyanzeng: Debounce(function() {
				let arry = this.array.join(' ');
				let data = {
					zjc: arry
				}
				let opts = {
					url: '/user/yzzjc',
					method: 'GET'
				};
				// console.log('打印入参',data);
				this.api.httpTokenRequest(opts, data).then(res => {
					// console.log('验证助记词', res);
					if (res[1].data.state == 'success') {
						this.$modal.msg(res[1].data.msg)
						if (this.type == 1) { //设置支付密码
							this.mode = 2
							this.showModal = true
						} else if (this.type == 2) { //修改支付密码
							this.mode = 3
							this.showModal = true
						}
					} else {
						this.$modal.msg(res[1].data.msg)
						setTimeout(() => {
							this.$modal.msg(this.$t('zhujici.zhujici13'))
							this.array = []
						}, 2000);
					}

				})


			}, 1000)
		},
	};
</script>

<style scoped>
	.container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		color: #000;
	}

	.contenter {
		width: 84%;
		padding: 20px;
		border-radius: 20rpx;
		background-color: #f0f0f0;
	}

	.contenter1 {
		width: 84%;
		padding: 20px;
		border-radius: 20rpx;
		background-color: #f0f0f0;
		display: flex;
		flex-direction: column;
		margin-top: 40rpx;
	}

	.content {
		width: 90%;
		padding: 10px;
		background-color: #f0f0f0;
		border-radius: 20rpx;
		margin-top: 20px;
	}

	.content-title {
		margin-top: 15rpx;
		margin-bottom: 15rpx;

	}

	.content-fuzhi {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #55aaff;
	}

	.row {
		width: 100%;
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
		margin-bottom: 10px;
	}

	.item {
		width: 21%;
		height: 60rpx;
		/* 每个项目占容器的 30% 宽度，确保一行有 3 个 */
		text-align: center;
		padding: 10px;
		margin: 15rpx;
		background-color: #f0f0f0;
		border-radius: 5px;
		box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
	}

	.anniu {
		width: 90%;
		height: 100rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #55aaff;
		color: #fff;
		margin-top: 40rpx;
		border-radius: 20rpx;
	}

	.anniu:active {
		border: 1rpx solid #55aaff;
		background-color: #fff;
		color: #55aaff;
	}
</style>